/**
 * Created by hxpeng on 2017/4/28.
 */
(function () {
    'use strict';

    angular.module('App.pages.member.bind')
        .controller('BindConfirmCtrl', BindConfirmCtrl);

    function BindConfirmCtrl(API,$scope,AppUtils,$location,StoreUtils,LoginAccountAuth,MemberModel) {


        var vm = this;

        LoginAccountAuth.getAccount()
            .then(function (data) {
                vm.memberVo = StoreUtils.getObj("bindMemberVo");
                vm.memberVo.openId = data.openId;

                vm.student = {};
                vm.class = {};

                if(vm.memberVo.memberType == "2"){
                    vm.student = StoreUtils.getObj("student");
                }else if(vm.memberVo.memberType == "1"){
                    vm.class = StoreUtils.getObj("class");
                }
            });




        $scope.postMemberInfo = function(){
            MemberModel.bindWeChatInfo(vm.memberVo).$promise.then(function(response){
                if(response.stat == 'err'){
                    AppUtils.showAlert(response.msg);
                }else{
                    var loginAccountVo = response.loginAccountVo;
                    LoginAccountAuth.setAccount(loginAccountVo);
                    AppUtils.showAlert("绑定成功！");
                    var gotoUrl = LoginAccountAuth.getLastGotoUrl();
                    if(!AppUtils.isUndefinedOrNull(gotoUrl)){
                        $location.path(gotoUrl);
                    }else{
                        if(loginAccountVo.memberType == 2){
                            $location.path("/member/parentIndex");
                        }else if(loginAccountVo.memberType == 1){
                            $location.path("/member/teacherIndex");
                        }
                    }

                }
            })
        }
        $scope.goBack = function(){
            if(vm.memberVo.memberType == "2"){
                $location.path("/bind/bindParentInfo");
            }else if(vm.memberVo.memberType == "1"){
                $location.path("/bind/bindTeacherInfo");
            }
        }

    }

})();
